package com.lanou.controller;

import com.lanou.bean.AddrInfo;
import com.lanou.service.AddrInfoService;
import com.lanou.service.CityService;
import com.lanou.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("addr")
public class AddrInfoController {

    @Autowired
    private AddrInfoService addrInfoService;

    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private CityService cityService;
    /**
     * 根据会员账号添加收货地址
     * @return
     */
    @RequestMapping("/addAddrInfoByVipNo.do")
    @ResponseBody
    public Map<String, Object> addAddrInfoByVipNo(AddrInfo addrInfo){
        System.out.println("前端获取的地址信息------"+addrInfo);
        addrInfo.setInstDate(new Date());
        addrInfo.setIsPrimary("n");
        addrInfo.setIsUsed("y");
        addrInfo.setInstId(1);
        addrInfo.setMdfDate(new Date());
        int result = addrInfoService.insertSelective(addrInfo);
        Map<String,Object> map=new HashMap<>();
        if(result>0){
            map.put("result","success");
            return map;
        }else {
            map.put("result","false");
            return map;
        }
    }


    /**
     *  根据地址id查询地址信息
     *
     * @author shw
     * @date 17/11/25 10:26
     */
    @RequestMapping("/selectAddrInfoByid.do")
    @ResponseBody
    public AddrInfo selectAddrInfoByid(Integer id){
        AddrInfo addrInfo = addrInfoService.selectAddrInfoByid(id);
        //根据省id查询省
        String province = provinceService.selectProvinceByP(addrInfo.getProvinceId());
        addrInfo.setProvince(province);
        //根据市id查询市
        String city = cityService.selectCityByC(addrInfo.getCityId());
        addrInfo.setCity(city);
        return addrInfo;
    }

}
